package com.example.asus.imtao;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.example.asus.imtao.Fragment.FindFragment;
import com.example.asus.imtao.Fragment.MainFragment;
import com.example.asus.imtao.Fragment.MeFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    protected LinearLayout mMenu_Main;
    protected LinearLayout mMenu_Find;
    protected LinearLayout mMenu_Me;
    protected MainFragment mMainFragment = new MainFragment();//首页
    protected FindFragment mFindFragment = new FindFragment();//发现
    protected MeFragment mMeFragment = new MeFragment();//我的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //添加Fragment
        LoadFragment();
        
        //初始化
        initViews();

        //设置监听器
        setListener();
    }

    //添加Fragment
    private void LoadFragment() {
        //获取管理类
        this.getSupportFragmentManager()
                //事务逻辑：
                //默认：显示首页 其他页面隐藏
                .beginTransaction()
                .add(R.id.container_content,mMainFragment)
                .add(R.id.container_content,mFindFragment)
                .hide(mFindFragment)
                .add(R.id.container_content,mMeFragment)
                .hide(mMeFragment)
                //提交
                .commit();
    }

    //初始化
    private void initViews() {
        mMenu_Main = (LinearLayout) findViewById(R.id.menu_home);
        mMenu_Find = (LinearLayout) findViewById(R.id.menu_find);
        mMenu_Me = (LinearLayout) findViewById(R.id.menu_me);
    }

    //设置监听器
    private void setListener(){
        mMenu_Main.setOnClickListener(this);
        mMenu_Find.setOnClickListener(this);
        mMenu_Me.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.menu_home://首页
                //获取管理类
                this.getSupportFragmentManager()
                        .beginTransaction()
                        .show(mMainFragment)//显示首页
                        .hide(mFindFragment)//隐藏其他页面
                        .hide(mMeFragment)
                        .commit();
                break;
            case R.id.menu_find://发现
                this.getSupportFragmentManager()
                        .beginTransaction()
                        .hide(mMainFragment)
                        .show(mFindFragment)
                        .hide(mMeFragment)
                        .commit();
                break;
            case R.id.menu_me://我的
                this.getSupportFragmentManager()
                        .beginTransaction()
                        .hide(mMainFragment)
                        .hide(mFindFragment)
                        .show(mMeFragment)
                        .commit();
                break;
        }
    }
}
